package com.rjwh.dingdong.client.mvp.presenter.impl;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;

import com.aishua.lib.task.AsResult;
import com.rjwh.dingdong.client.mvp.BasePresenter;
import com.rjwh.dingdong.client.mvp.presenter.IRadioLocalPlayListPresenter;
import com.rjwh.dingdong.client.mvp.view.RadioLocalPlayListView;
import com.rjwh.dingdong.client.service.PlaybackService;

/**
 * @author Kay
 * @ClassName RadioLocalLocalPlayListPresenter
 * @Description
 * @date 2016-10-28
 * @history 2016-10-28 author: description:
 */

public class RadioLocalLocalPlayListPresenter extends BasePresenter<RadioLocalPlayListView> implements IRadioLocalPlayListPresenter {

    private PlaybackService mPlaybackService;
    private boolean mIsServiceBound;

    public RadioLocalLocalPlayListPresenter(Context context, RadioLocalPlayListView mMvpView) {
        super(context, mMvpView);
    }

    @Override
    public void onServerDataAcquired(int businessType, AsResult mAsResult, boolean isRefresh) {

    }

    @Override
    public void bindPlaybackService() {
        getContext().bindService(new Intent(getContext(), PlaybackService.class), mConnection, Context.BIND_AUTO_CREATE);
        mIsServiceBound = true;
    }

    @Override
    public void unbindPlaybackService() {
        if (mIsServiceBound) {
            // Detach our existing connection.
            getContext().unbindService(mConnection);
            mIsServiceBound = false;
        }
    }

    @Override
    public void subscribe() {
        bindPlaybackService();

        if (mPlaybackService != null && mPlaybackService.isPlaying()) {
            getMvpView().onSongUpdated(mPlaybackService.getPlayingSong());
        }
    }

    @Override
    public void detachView() {
        unbindPlaybackService();
        detachViews();
    }

    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            // This is called when the connection with the service has been
            // established, giving us the service object we can use to
            // interact with the service.  Because we have bound to a explicit
            // service that we know is running in our own process, we can
            // cast its IBinder to a concrete class and directly access it.
            mPlaybackService = ((PlaybackService.LocalBinder) service).getService();
            if (mPlaybackService != null) {
                getMvpView().onPlaybackServiceBound(mPlaybackService);
                getMvpView().onSongUpdated(mPlaybackService.getPlayingSong());
                getMvpView().updatePlayList(mPlaybackService.getPlayList().getSongs());
                getMvpView().updatePlayToggle(mPlaybackService.getPlayStatus());
            }
        }

        public void onServiceDisconnected(ComponentName className) {
            mPlaybackService = null;
            getMvpView().onPlaybackServiceUnbound();
        }
    };

}
